﻿using Leadtools;
using Leadtools.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SteelDetection.UIControl
{
    public class LightContrastInteractiveMode : ImageViewerInteractiveMode
    {
        public LightContrastInteractiveMode() :
              base()
        {
            this.AutoItemMode = ImageViewerAutoItemMode.AutoSet;
        }

        private Keys _scaleKeyModifier = Keys.Control;
        public virtual Keys ScaleKeyModifier
        {
            get { return _scaleKeyModifier; }
            set
            {
                // Supported is none and any modifiers 
                switch (value)
                {
                    case Keys.None:
                    case Keys.Alt:
                    case Keys.Shift:
                    case Keys.Control:
                        _scaleKeyModifier = value;
                        break;

                    default:
                        throw new ArgumentException("Invalid value", "ScaleKeyModifier");
                }
            }
        }

        private Keys _rotateKeyModifier = Keys.Alt;
        public virtual Keys RotateKeyModifier
        {
            get { return _rotateKeyModifier; }
            set
            {
                // Supported is none and any modifiers 
                switch (value)
                {
                    case Keys.None:
                    case Keys.Alt:
                    case Keys.Shift:
                    case Keys.Control:
                        _rotateKeyModifier = value;
                        break;

                    default:
                        throw new ArgumentException("Invalid value", "PageKeyModifier");
                }
            }
        }

        public override string Name
        {
            get { return "LightContrast"; }
        }

        public override int Id
        {
            get { return ImageViewerInteractiveMode.UserModeId + 1; }
        }

        public override void Start(ImageViewer imageViewer)
        {
            base.Start(imageViewer);
            var service = base.InteractiveService;
            // Pan required events 
            service.DragStarted += new EventHandler<InteractiveDragStartedEventArgs>(service_DragStarted);
            service.DragDelta += new EventHandler<InteractiveDragDeltaEventArgs>(service_DragDelta);
            service.DragCompleted += new EventHandler<InteractiveDragCompletedEventArgs>(service_DragCompleted);
            service.DoubleTap += new EventHandler<InteractiveEventArgs>(service_DoubleTap);
        }

        public override void Stop(ImageViewer imageViewer)
        {
            if (IsStarted)
            {
                var service = base.InteractiveService;

                service.DragStarted -= new EventHandler<InteractiveDragStartedEventArgs>(service_DragStarted);
                service.DragDelta -= new EventHandler<InteractiveDragDeltaEventArgs>(service_DragDelta);
                service.DragCompleted -= new EventHandler<InteractiveDragCompletedEventArgs>(service_DragCompleted);
                service.DoubleTap -= new EventHandler<InteractiveEventArgs>(service_DoubleTap);

                base.Stop(imageViewer);
            }
        }

        private void service_DragStarted(object sender, InteractiveDragStartedEventArgs e)
        {
            // Make sure pinch is not working, otherwise, ignore pan 
            if (!this.CanStartWork(e))
                return;

            // Make sure we are on an item 
            if (this.Item == null)
                return;

            e.IsHandled = true;
            this.OnWorkStarted(EventArgs.Empty);
        }

        private void service_DragDelta(object sender, InteractiveDragDeltaEventArgs e)
        {
            //if (!this.IsWorking)
            //    return;

            var item = this.Item;
            //if (item == null)
            //    return;

            // Find out what to do 
            var dx = e.Change.X;
            var dy = e.Change.Y;

            if (dx == 0 && dy == 0)
                return;
            if (dy != 0)
                AdjustLight(this.ImageViewer, item, dy);
            if (dx != 0)
                AdjustContrast(this.ImageViewer, item, dx);
            e.IsHandled = true;
        }

        private void service_DragCompleted(object sender, InteractiveDragCompletedEventArgs e)
        {
            if (!this.IsWorking)
                return;

            e.IsHandled = true;
            OnWorkCompleted(EventArgs.Empty);
        }

        private void service_DoubleTap(object sender, InteractiveEventArgs e)
        {
            // Make sure we not working already 
            if (!this.CanStartWork(e))
                return;

            // Make sure we have an item 
            var item = this.Item;
            //if (item == null)
            //    return;

            e.IsHandled = true;

            this.OnWorkStarted(EventArgs.Empty);
            Identity(this.ImageViewer);
            this.OnWorkCompleted(EventArgs.Empty);
        }

        private void Identity(ImageViewer imageViewer)
        {
            imageViewer.Image.PaintIntensity = 0;
            imageViewer.Image.PaintContrast = 0;
        }

        private void AdjustLight(ImageViewer imageViewer, ImageViewerItem item, int dy)
        {
            var image = imageViewer.Image;
            imageViewer.Image.PaintIntensity+=dy;
        }

        private void AdjustContrast(ImageViewer imageViewer, ImageViewerItem item, int dx)
        {
            var image = imageViewer.Image;
            imageViewer.Tag = image;
            imageViewer.Image.PaintContrast += dx;
        }

        private void Translate(ImageViewer imageViewer, ImageViewerItem item, int dx, int dy)
        {
            // ... 
            // set translate code 
            // ... 
        }
    }
}
